package com.hy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hy.common.Result;
import com.hy.entity.Product;
import com.hy.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;


/**
 * 商品管理
 */
@RestController
@RequestMapping("/products")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 获取商品列表
     */
    @GetMapping("/page")
    public Result<Page<Product>> list(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String name) {
        Page<Product> page = new Page<>(current, size);
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        if (status != null) {
            wrapper.eq(Product::getStatus, status);
        }
        if (name != null) {
            wrapper.like(Product::getName, name);
        }

        return Result.success(productService.page(page, wrapper));
    }

    /**
     * 获取商品详情
     */
    @GetMapping("/{id}")
    public Result<Product> getById(@PathVariable Long id) {
        return Result.success(productService.getById(id));
    }

    /**
     * 添加商品
     */
    @PostMapping
    public Result<Boolean> add(@RequestBody Product product) {
        return Result.success(productService.save(product));
    }

    /**
     * 修改商品
     */
    @PutMapping("/{id}")
    public Result<Boolean> update(@PathVariable Long id, @RequestBody Product product) {
        product.setId(id);
        return Result.success(productService.updateById(product));
    }

    /**
     * 删除商品
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        return Result.success(productService.removeById(id));
    }

    /**
     * 商品上架
     */
    @PutMapping("/{id}/on-sale")
    public Result<Boolean> putOnSale(@PathVariable Long id) {
        return Result.success(productService.putOnSale(id));
    }

    /**
     * 商品下架
     */
    @PutMapping("/{id}/off-sale")
    public Result<Boolean> putOffSale(@PathVariable Long id) {
        return Result.success(productService.putOffSale(id));
    }

    /**
     * 更新库存
     */
    @PutMapping("/{id}/stock")
    public Result<Boolean> updateStock(@PathVariable Long id, @RequestParam Integer stock) {
        return Result.success(productService.updateStock(id, stock));
    }

    /**
     * 兑换商品
     */
    @PostMapping("/{id}/exchange")
    public Result<Boolean> exchange(@PathVariable Long id, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        return Result.success(productService.exchange(id, userId));
    }

    /**
     * 分页查询商品列表
     * @param pageNum 页码，默认1
     * @param pageSize 每页数量，默认10
     * @param status 状态（0-下架，1-上架），可选
     * @return 分页结果
     */
    @GetMapping("/admin/page")
    public Result<Page<Product>> pageProducts(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String name
    ) {
        Page<Product> page = productService.pageProducts(pageNum, pageSize, status,name);
        return Result.success(page);
    }
    // 新增商品
    @PostMapping("/admin/add")
    public Result addProduct(@RequestBody Product product) {
        boolean success = productService.saveProduct(product);
        return success ? Result.success() : Result.error(500, "新增商品失败");
    }

    // 更新商品
    @PutMapping("/admin/update")
    public Result updateProduct(@RequestBody Product product) {
        boolean success = productService.updateProduct(product);
        return success ? Result.success() : Result.error(500, "更新商品失败");
    }

    // 删除商品
    @DeleteMapping("/admin/{id}")
    public Result deleteProduct(@PathVariable Long id) {
        boolean success = productService.removeById(id);
        return success ? Result.success() : Result.error(500, "删除商品失败");
    }

    // 根据ID查询商品
    @GetMapping("/admin/{id}")
    public Result<Product> getProduct(@PathVariable Long id) {
        Product product = productService.getById(id);
        return product != null ? Result.success(product) : Result.error(404, "商品不存在");
    }

    // 商品上下架
    @PutMapping("/admin/status/{id}")
    public Result updateProductStatus(@PathVariable Long id, @RequestParam Integer status) {
        boolean success = productService.updateProductStatus(id, status);
        return success ? Result.success() : Result.error(500, "更新商品状态失败");
    }
}
